package api;

import com.alibaba.fastjson.JSON;
import domain.Notice;
import service.NoticeServlet;
import utils.Response;
import utils.ResponseCode;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;

public class GetNoticeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        分层:api接受请求，提取请求携带参数，返回消息，service：业务处理，dao：数据库访问；
        NoticeServlet noticeServlet = new NoticeServlet();
        Response build=null;
        Notice notice=null;
        try {
            notice=noticeServlet.getRecNotice();
            build = Response.build(ResponseCode.SUCCESS,notice);
        } catch (SQLException e) {//登录异常，返回异常消息
            e.printStackTrace();
            build = Response.build(ResponseCode.ERROR_DB);
        }
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSON(build).toString());
    }

    }


